package com.ztgf.commodity.api.cms;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.bo.AttributesQueryBO;
import com.ztgf.commodity.models.service.ICommoditySpecificationService;
import com.ztgf.commodity.models.vo.CommoditySpecificationVO;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-05-26 14:26
 * Description:商品规格
 */
@RestController
@RequestMapping("/cms/commoditySpecification")
@Slf4j
@Api(value = "CommoditySpecificationController", tags = "商品规格表接口")
public class CommoditySpecificationController {

    @DubboReference
    private ICommoditySpecificationService commoditySpecificationService;

    @PostMapping(value = "/insert")
    @ApiOperation(value = "添加商品规格表", notes = "添加商品规格表")
    public Result insert(String object) {
        if (!StringUtils.isNotBlank(object)) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            Result<List<String>> result = commoditySpecificationService.newAdd(object);
            return new Result<>(result);
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改商品规格表", notes = "修改商品规格表")
    public Result update(String object) {
        if (!StringUtils.isNotBlank(object)) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            Result<List<String>> result = commoditySpecificationService.newUpdate(object);
            return new Result<>(result);
        }
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除商品规格表(物理删除)", notes = "删除商品规格表(物理删除)")
    @ApiImplicitParam(name = "id", value = "商品规格表id", required = true, paramType = "query", dataType = "Long")
    public Result delete(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = commoditySpecificationService.deleteById(id);
        if (flag) {
            return new Result<>();
        }
        return new Result<>(CodeEnum.FAIL_100009);
    }

    @PostMapping("/deleteLogic")
    @ApiOperation(value = "删除/恢复商品规格表(逻辑删除)", notes = "删除/恢复商品规格表(逻辑删除)")
    @ApiImplicitParam(name = "id", value = "商品规格表id", required = true, paramType = "query", dataType = "Long")
    public Result deleteLogic(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = commoditySpecificationService.deleteLogic(id);
        if (flag) {
            return new Result<>();
        }
        return new Result<>(CodeEnum.FAIL_100009);
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除商品规格表", notes = "批量删除商品规格表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        commoditySpecificationService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/getBySpecId")
    @ApiOperation(value = "根据id查找商品规格表", notes = "根据id查找商品规格表")
    @ApiImplicitParam(name = "id", value = "商品规格表id", required = true, paramType = "query", dataType = "Long")
    public Result getById(Long id) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            CommoditySpecificationVO vo = commoditySpecificationService.getById(id);
            return new Result<>(vo);
        }
    }

    @PostMapping("/queryPageList")
    @ApiOperation(value = "商品规格列表", notes = "商品规格列表")
    public Result queryPageList(AttributesQueryBO bo) {
        Page<CommoditySpecificationVO> list = commoditySpecificationService.newQueryPageList(bo);
        return new Result<>(list);
    }

    @PostMapping("/newAdd")
    @ApiOperation(value = "新的添加规格属性接口", notes = "新的添加规格属性接口")
    public Result newAdd(String object) {
        if (!StringUtils.isNotBlank(object)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<>(commoditySpecificationService.newAdd(object));
    }

    @PostMapping("/newUpdate")
    @ApiOperation(value = "新的修改规格属性接口", notes = "新的修改规格属性接口")
    public Result newUpdate(String object) {
        if (!StringUtils.isNotBlank(object)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<>(commoditySpecificationService.newUpdate(object));
    }

}
